package com.autotest.glin.util;

import com.autotest.glin.asserts.RespondAssertForJson;
import com.google.gson.Gson;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
 *根据测试用例文件拼装报文body(json格式)
 * 输入一贯改造_短信发送.xls，return json串列表
 */
public class AssembledMetaForJson {
    static Logger log = Logger.getLogger(AssembledMetaForJson.class);
    static Map<String, Object> bodyMap = new HashMap<String, Object>();
    static Map<String, Object> dataMap = new HashMap<String, Object>();
    static Map<String, Object> caseMessMap = new HashMap<String, Object>();
    static Map<String, String> map = new HashMap<String, String>();

    public static void getMap(Sheet sheet, int cols, int row) {
        for (int col = 0; col < cols; col++) {
            String cellKey = sheet.getCell(col, 0).getContents();// 表头
            String cellValue = sheet.getCell(col, row).getContents();// 值ֵ
            if (col >= 5) {
                dataMap.put(cellKey, RespondAssertForJson.getBuildValue("", sheet.getCell(col, row).getContents()));
            } else {
                caseMessMap.put(cellKey, cellValue);
            }
        }
        bodyMap.put("data", dataMap);
        map.put(new Gson().toJson(caseMessMap), new Gson().toJson(bodyMap));
    }

    public static Map<String, String> assembleMeta(String fileName, String caseNo)
            throws IOException, BiffException, IOException, BiffException {
        log.info("文件名：" + fileName);

        String filePath = new GetFileMeta().getFilePath("testCase", fileName);
        File xlsFile = new File(filePath);
        Workbook workbook = Workbook.getWorkbook(xlsFile); // 获得工作簿对象
        Sheet sheet = workbook.getSheet(0); // 获得工作表
        int rows = sheet.getRows(); // 获得行数
        int cols = sheet.getColumns(); // 获得列数

        bodyMap.clear();
        dataMap.clear();
        caseMessMap.clear();

        for (int row = 1; row < rows; row++) {
            String yOn = sheet.getCell(3, row).getContents();
            String caseNo1 = sheet.getCell(0, row).getContents().toLowerCase(); // 文件中的案例编号
            if (yOn.equals("Y") && caseNo.equals("")) { // 获取全部为Y的案例报文体
                getMap(sheet, cols, row);
            } else if (caseNo1.equals(caseNo.toLowerCase())) { // 获取单条案例报文体，不管执行标识是否为Y
                getMap(sheet, cols, row);
            }
        }

        workbook.close();
        return map;
    }
}
